package com.example.androidshixun.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.example.androidshixun.R;

public class ViewPagerIndicator extends LinearLayout implements View.OnClickListener {

    private int mCount; //总页面数量
    private int mCurrent; //当前页码
    private ViewPagerIndicatorOnClickListener mlistener;

    public ViewPagerIndicator(Context context,
                              @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public void setCount(int count) {
        mCount = count;
    }

    public void setCurrent(int curr) {
        removeAllViews();
        for(int i=0; i<mCount;++i){
            ImageView circle = new ImageView(getContext());
            if(curr == i)
                circle.setImageResource(R.drawable.indicator_on);
            else
                circle.setImageResource(R.drawable.indicator_off);
            circle.setPadding(  5,0,5,0  );
            circle.setTag(i);
            circle.setOnClickListener(this);
            addView(circle);
        }
    }

    public void setOnClickListener(ViewPagerIndicatorOnClickListener listener) {
        mlistener = listener;
    }


    @Override
    public void onClick(View v) {
        Integer i = (Integer)v.getTag();
        Log.d(  this.getClass().toString()  ,  i + "" );
        mlistener.onClick(i);
    }
}
